package com.kevin.permission.filter;

import com.kevin.permission.common.RequestHolder;
import com.kevin.permission.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import sun.misc.Request;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Slf4j
public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;

//        log.info("enter there......."+req.getRequestURI());
        SysUser sysUser= (SysUser) req.getSession().getAttribute("user");
        if (sysUser==null){
            String path="/signin.jsp?ret="+req.getRequestURI();
            resp.sendRedirect(path);
            return;
        }

        RequestHolder.add(sysUser);
        RequestHolder.add(req);
        filterChain.doFilter(servletRequest,servletResponse);
        return;
    }

    public void destroy() {

    }
}
